package de.dlrg.wms.gui.forms.generic;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import de.dlrg.wms.gui.views.coulmnNames.SwimmerColumnNames;

import swing2swt.layout.BorderLayout;

public abstract class AbstractTableView<T> extends Composite {
	protected Class<?> tableColumns;
	protected Table table;
	protected Shell shell;

	protected Composite center;

	protected abstract void refresh();

	protected final String[] getColumnNames() {
		this.tableColumns.getEnumConstants();
		String ret[] = new String[this.tableColumns.getEnumConstants().length];
		for (Object columnName : this.tableColumns.getEnumConstants()) {
			ret[((Enum<?>)columnName).ordinal()] = columnName.toString();
		}
		return ret;
	}

	public AbstractTableView(Composite arg0, int arg1) {
		super(arg0, arg1);
		setLayout(new BorderLayout(0, 0));

		center = new Composite(this, SWT.NONE);
		center.setLayoutData(BorderLayout.CENTER);

		table = new Table(this.center, SWT.BORDER | SWT.FULL_SELECTION
				| SWT.MULTI);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
	}

	protected void initGUI(final Shell shell) {
		this.shell = shell;
		this.center.setLayout(new GridLayout(1, false));
		this.setColumnNames(this.getColumnNames());

		this.refresh();
		this.resizeTable();
	}

	private void setColumnNames(String[] columnNames) {
		for (String columnName : columnNames) {
			TableColumn tblclmnid = new TableColumn(table, SWT.NONE);
			tblclmnid.setWidth(150);
			tblclmnid.setText(columnName);
		}
	}

	private void resizeColumn(TableColumn tableColumn_) {
		tableColumn_.pack();

	}

	private void resizeTable() {
		for (TableColumn tc : table.getColumns())
			resizeColumn(tc);
	}
}
